package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.comprehensive.domain.*;
import com.ruoyi.comprehensive.dto.FirePreventionExamineDto;
import com.ruoyi.comprehensive.mapper.FirePreventionMapper;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.vo.FireVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.FirePreventionExamineMapper;
import com.ruoyi.comprehensive.service.IFirePreventionExamineService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 防火审核Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-16
 */
@Service
public class FirePreventionExamineServiceImpl implements IFirePreventionExamineService 
{
    @Autowired
    private FirePreventionExamineMapper firePreventionExamineMapper;
    @Autowired
    private FirePreventionMapper firePreventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    /**
     * 查询防火审核
     * 
     * @param id 防火审核主键
     * @return 防火审核
     */
    @Override
    public FirePreventionExamine selectFirePreventionExamineById(String id)
    {
        return firePreventionExamineMapper.selectFirePreventionExamineById(id);
    }

    /**
     * 查询防火审核列表
     * 
     * @param firePreventionExamine 防火审核
     * @return 防火审核
     */
    @Override
    public List<FirePreventionExamine> selectFirePreventionExamineList(FirePreventionExamine firePreventionExamine)
    {
        return firePreventionExamineMapper.selectFirePreventionExamineList(firePreventionExamine);
    }

    /**
     * 新增防火审核
     * 
     * @param firePreventionExamine 防火审核
     * @return 结果
     */
    @Override
    public int insertFirePreventionExamine(FirePreventionExamine firePreventionExamine)
    {
        firePreventionExamine.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return firePreventionExamineMapper.insertFirePreventionExamine(firePreventionExamine);
    }

    /**
     * 修改防火审核
     * 
     * @param firePreventionExamine 防火审核
     * @return 结果
     */
    @Override
    public int updateFirePreventionExamine(FirePreventionExamine firePreventionExamine)
    {
        FirePreventionExamine firePreventionExamine1 = new FirePreventionExamine();
        BeanUtils.copyProperties(firePreventionExamine,firePreventionExamine1);
        firePreventionExamine1.setUpdateAt(new Date());
        firePreventionExamine1.setUpdateByName(SecurityUtils.getUsername());
        firePreventionExamine1.setUpdateBy(SecurityUtils.getUserId().toString());
        FirePrevention firePrevention = firePreventionMapper.selectFirePreventionById(firePreventionExamine.getFirePreventionId());
        if (firePrevention != null){
            FirePrevention firePrevention1 = new FirePrevention();
            firePrevention1.setUpdateAt(new Date());
            firePrevention1.setUpdateByName(SecurityUtils.getUsername());
            firePrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
            firePrevention1.setExamineStatus(firePreventionExamine.getExamineStatus());
            firePreventionMapper.updateFirePrevention(firePrevention1);
        }
        return firePreventionExamineMapper.updateFirePreventionExamine(firePreventionExamine1);
    }

    /**
     * 批量删除防火审核
     * 
     * @param ids 需要删除的防火审核主键
     * @return 结果
     */
    @Override
    public int deleteFirePreventionExamineByIds(String[] ids)
    {
        return firePreventionExamineMapper.deleteFirePreventionExamineByIds(ids);
    }

    /**
     * 删除防火审核信息
     * 
     * @param id 防火审核主键
     * @return 结果
     */
    @Override
    public int deleteFirePreventionExamineById(String id)
    {
        return firePreventionExamineMapper.deleteFirePreventionExamineById(id);
    }

    /**
     * 获取防火审核列表
     * @param vo
     * @return
     */
    @Override
    public PageInfo<FirePreventionExamineDto> getFirePreventionExamineList(FireVo vo) {
        // 开始分页
        startPage();
        List<FirePreventionExamine> firePreventionExamineList = firePreventionExamineMapper.getFirePreventionExamineList(vo);
        //  获取分页信息
        PageInfo fireExamineDtoInfo = new PageInfo<>(firePreventionExamineList);
        List<FirePreventionExamineDto> list = firePreventionExamineList.stream().map(firePreventionExamine -> {
            FirePreventionExamineDto firePreventionExamineDto = new FirePreventionExamineDto();
            BeanUtils.copyProperties(firePreventionExamine, firePreventionExamineDto);
            FirePrevention firePrevention = firePreventionMapper.selectFirePreventionById(firePreventionExamine.getFirePreventionId());
            firePreventionExamineDto.setFirePreventionDate(firePrevention.getFirePreventionDate());
            firePreventionExamineDto.setFirePreventionCode(firePrevention.getFirePreventionCode());
            firePreventionExamineDto.setName(firePrevention.getName());
            return firePreventionExamineDto;
        }).collect(Collectors.toList());
        // 将封装后的列表放到分页对象中
        fireExamineDtoInfo.setList(list);
        return fireExamineDtoInfo;
    }

}
